package com.mhkj.revise.plan.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.busiMode.service.BusiModelService;
import com.mhkj.base.busiType.service.BusinessTypeService;
import com.mhkj.base.itemInfo.item.service.ItemService;
import com.mhkj.revise.plan.entity.PlanNode;
import com.mhkj.revise.plan.service.PlanNodeService;
import com.mics.core.common.componet.JsonHandler;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.bean.Office;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 计划节点
 * 
 * @author mics
 * @date 2018年7月6日
 * @version 1.0
 */
@Controller
@RequestMapping("/planNode")
public class PlanNodeController extends BaseController<PlanNode> {
	@Resource
	private PlanNodeService planNodeService;
	@Resource
	private BusinessTypeService businessTypeService;
	@Resource
	private ItemService itemService;
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	private OrgService orgService;
	@Resource
	private BusiModelService busiModelService;
	@Resource
	private JsonHandler jsonHandler;

	@Resource
	public void setPlanNodeService(PlanNodeService planNodeService) {
		super.setBaseService(planNodeService);
	}

	@InitBinder
	protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}

	@RequestMapping("/listByPage")
	@ResponseBody
	public void listByPage(HttpServletResponse response, PlanNode planNode, int pageIndex, int pageSize) {
		Pagination<PlanNode> pagination = planNodeService.listByPage(planNode, pageIndex, pageSize);
		jsonHandler.sendJsonString(response, true, "获取数据成功", pagination,new String[]{"id","NO","business.typeName","nodeName","nodeProperty.value",
				"completeStandard","deadline","deadlineDesc","beginDate","endDate","remark","companyName","officeName","postName"},
				"id","NO","business","nodeName","nodeProperty","completeStandard","deadline","deadlineDesc","beginDate","endDate","remark","companyName","officeName","postName");
	}


	
	@Override
	@RequestMapping("/save")
	@ResponseBody
	public ResponseMessage save(PlanNode t) {
		String message = planNodeService.save(t);
		if (message == null) {
			return ResponseMessage.createMessage(true, "保存计划节点成功！");
		} else {
			return ResponseMessage.createMessage(false, message);
		}
	}

	@Override
	@RequestMapping("/edit")
	@ResponseBody
	public ResponseMessage edit(PlanNode t) {
		return super.edit(t);
	}

	@Override
	@RequestMapping("/del")
	@ResponseBody
	public ResponseMessage del(PlanNode t) {
		return super.del(t);
	}

	@Override
	@RequestMapping("/info")
	@ResponseBody
	public PlanNode info(long id) {
		PlanNode planNode = super.info(id);
		Office cOffice = orgService.officeInfo(planNode.getOfficeId());
		planNode.setOfficeName(cOffice.getName());
		return planNode;
	}

	/**
	 * 设置负责人
	 * 
	 * @author mics
	 * @date 2018年7月9日
	 * @version 1.0
	 */
	@RequestMapping("/grantManager")
	@ResponseBody
	public ResponseMessage grantManager(long planNodeId, String managerId) {
		PlanNode planNode = planNodeService.getById(planNodeId);
		return super.edit(planNode);
	}

	/**
	 * 查询所有的计划节点
	 * 
	 * @author mics
	 * @date 2018年7月11日
	 * @version 1.0
	 */
	@RequestMapping("/listAll")
	@ResponseBody
	public List<PlanNode> listAll() {
		List<PlanNode> planNodes = planNodeService.queryBusiSysPlan();
		return planNodes;
	}

	@RequestMapping("/countlistByPage")
	@ResponseBody
	public Pagination<Object> countlistByPage(HttpServletResponse response, PlanNode planNode, int pageIndex,
			int pageSize) {
		List<Object> countList = planNodeService.countList();
		Pagination<Object> pagination = new Pagination<>();
		List<Object> list = new ArrayList<>();
		for (Object row : countList) {
			Object[] rowArray = (Object[]) row;
			Employee manager = orgService.employeeInfo(rowArray[8].toString());
			Map<String, Object> mapArr = new HashMap<String, Object>();
			mapArr.put("name", rowArray[0]);
			mapArr.put("value", rowArray[1]);
			mapArr.put("overDay", rowArray[2]);
			mapArr.put("overTasks", rowArray[3]);
			mapArr.put("tasks", rowArray[4]);
			mapArr.put("ended", rowArray[5]);
			mapArr.put("doing", rowArray[6]);
			mapArr.put("important", rowArray[7]);
			mapArr.put("managerName", manager.getName());
			mapArr.put("managerNo", rowArray[9]);
			list.add(mapArr);
		}
		pagination.setTotal(list.size());
		pagination.setData(list);

		return pagination;
	}

}
